package com.arges.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.Properties;

@Configuration // 表示这是一个配置类
@ComponentScan("com.arges") // 包扫描
@PropertySource("classpath:jdbc.properties")//加载配置文件
@PropertySource("classpath:hibernate.properties")//加载配置文件
@EnableTransactionManagement(proxyTargetClass = true) // 开启Spring的注解配置事务
public class SpringConfig {


    @Value("${jdbc.class.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;

    @Value("${hibernate.dialect}")
    private String dialect;
    @Value("${hibernate.show_sql}")
    private String show_sql;
    @Value("${hibernate.format_sql}")
    private String format_sql;
    @Value("${hibernate.hbm2ddl.auto}")
    private String hbm2ddl;

    /**
     * jdbc 配置 Druid数据源连接池
     * @return
     */
    @Bean
    public DataSource dataSource(){
        DruidDataSource ds = new DruidDataSource();
        ds.setDriverClassName(driver);
        ds.setUrl(url);
        ds.setUsername(username);
        ds.setPassword(password);
        return ds;
    }

    /**
     *  hibernate 配置 创建sessionFactory工厂
     */
    @Bean(name = "sessionFactory")
    public SessionFactory getSessionFactory(@Autowired DataSource dataSource) {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        // 添加Druid数据源
        sessionFactory.setDataSource(dataSource);
        Properties hibernateProperties = new Properties();

        // 加载hibernate的基本配置
        hibernateProperties.put("hibernate.dialect",dialect);
        hibernateProperties.put("hibernate.show_sql",show_sql);
        hibernateProperties.put("hibernate.format_sql",format_sql);
        hibernateProperties.put("hibernate.hbm2ddl.auto",hbm2ddl);
        // 扫描带有注解的实体类的包
        sessionFactory.setPackagesToScan("com.arges.pojo");
        try {
            sessionFactory.afterPropertiesSet();// 一定要加这句话，通过查看源码得知：调用afterPropertiesSet()方法：//在这里开始生成一个真正的SessionFactory对象；然后利用getObjecy生成出来
            // 如果没有这个方法就不能产生sessionFactory对象，就不能通过getObject()方法获得新创建的对象sessionFactory
        } catch (IOException e) {
            e.printStackTrace();
        }

//        // 加载映射文件
//        String[] mappingResources = new String[]{"Book.hbm.xml"," User.hbm.xml"};
//        // 添加映射文件
//        sessionFactory.setMappingResources(mappingResources);
        return sessionFactory.getObject();
    }

    /**
     * 事务
     * 事务管理器
     */
    // 配置事务
    @Bean(name = "txManager")
    public HibernateTransactionManager getTransactionManager(@Autowired DataSource dataSource, @Autowired SessionFactory sessionFactory) {
        HibernateTransactionManager transactionManager = new HibernateTransactionManager();
        transactionManager.setDataSource(dataSource);
        transactionManager.setSessionFactory(sessionFactory);
        return transactionManager;
    }




}
